<template>
<div class="login">
    <div class="login-container">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
        <h2 style="text-align: center; padding: 10px 0">乐富教育管理系统</h2>
        <el-form-item label="用户名:" label-width="70px" prop="username">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.username" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item label="密 码:" label-width="70px" prop="password">
          <el-input
          prefix-icon="el-icon-view"
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            @keyup.enter.native="login"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleLogin" type="primary" style="width: 100%" >登录</el-button>
          <div class="reg" @click="register">前往注册</div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message:'请输入用户名',validator: validatePassword }],
        password: [{ required: true, trigger: 'blur', message:'请输入密码',validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) { 
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
     register() {
      this.$router.push('/register')
    }
  }
}
</script>

<style scoped lang="less">
.login {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("../../assets/images/bg.jpg") no-repeat;
  background-size: cover;

  .login-container {
    border-radius: 5px;
    width: 500px;
    background: rgba(255,255,255,0.8);
    padding: 10px 80px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    .reg {
     //height: 100px;
      cursor: pointer;
      float: right;
      //background-color: aquamarine;
      font-size: 18px;
      text-decoration: underline;
      color: red;
      top: 10px;
      left: 20px;
    }
  }
}
</style>
